/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.server;

import java.util.HashMap;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import cacao.client.CaCaOConstants;
import cacao.client.CaCaOSessionService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class CaCaOSessionServiceImpl extends RemoteServiceServlet implements CaCaOSessionService {


	private static final long serialVersionUID = -1723678908322216022L;

	public String getAttributeAsString(String key) {
		Object o = getThreadLocalRequest().getSession().getAttribute(key);
		if(o instanceof String){
			return (String)o;
		}
		return null;
	}

	/**
	 * Login Method
	 * @param userid
	 * @param password
	 * @return null if Login was successful, false otherwise
	 */
	public String loginEditor(String userid, String password) {
		String result = null;
		HttpSession session = this.getThreadLocalRequest().getSession();
		Properties cacaoProps = CaCaOServer.getInstance().getProperties();
		if(cacaoProps.getProperty("CaCaO.server.editors.ids").contains(userid) &&
				password.equals(cacaoProps.getProperty("CaCaO.server.editors.passwords."+userid).trim())){
			session.setAttribute(CaCaOConstants.SESSIONATTR_EDITOR_STRING, userid);
		}
		else{
			session.setAttribute(CaCaOConstants.SESSIONATTR_EDITOR_STRING, null);
			result = "Login failed";
		}
		return result;
	}

	/**
	 * @return the properties, which are used on the client side
	 */
	public HashMap<String, String> retrieveClientProperties(){
		HashMap<String, String> clientProps = new HashMap<String, String>();
		Properties props = CaCaOServer.getInstance().getProperties();
		for(Object o: props.keySet()){
			String s = (String)o;
			if(s.startsWith("CaCaO.client.")){
				clientProps.put(s, props.getProperty(s));
			}
		}
		return clientProps;
	}
}
